<?php
class MY_Model extends Model{
	
	var $_table = "elements";
	var $_table_meta = "elements_meta";
	var $_field_meta_rel = "element_id";
	
	var $_dateFormat = "%Y-%m-%d %H:%i:%s";
	
	function MY_Model(){
		parent::Model();
	}
	
	
	function getValidUrlName($name){
		if(!$this->isValidUrlName($name)){
			$tmpName = $name."-2";
			for($i=3; !$this->isValidUrlName($tmpName); $i++){
				$tmpName = $name . "-" . $i;
			}
			$name = $tmpName;
		}

		return $name;
	}

	function isValidUrlName($name){
		$this->db->select ( "id" );
		$this->db->from ( $this->_table );
		$this->db->where ( "url_name", $name );
		
		$query = $this->db->count_all_results ();
		
		return $query == 0;
	}
	
	
	/*
	 * Modelado tablas metas
	 */
	
	function addMeta($element_id, $key, $value, $single = FALSE){
		$valid = TRUE;
		if($single){
			$meta_value = $this->getMeta($element_id, $key, TRUE);
			
			if($meta_value != NULL){
				$valid = FALSE;
			}
		}
		
		if($valid){
			$data = array (
				$this->_field_meta_rel	=> $element_id,
				'meta_key'				=> $key,
				'meta_value' 			=> $value
			);
	
			$this->db->insert($this->_table_meta, $data);
			
			return $this->db->insert_id();
		}
		
		return FALSE;
	}
	
	function updateMeta($element_id, $key, $value, $pre_value = NULL){
		$this->db->where($this->_field_meta_rel, $element_id);
		$this->db->where("meta_key", $key);
		if($pre_value !== NULL){
			$this->db->where("meta_value", $pre_value);
		}
		
		$data = array(
			"meta_value" => $value
		);
		
		$this->db->update($this->_table_meta, $data);
	}
	
	function getMeta($element_id, $key = NULL, $single = FALSE){
		$this->db->from($this->_table_meta);
		$this->db->where($this->_field_meta_rel, $element_id);
		
		if($key != NULL){
			$this->db->where("meta_key", $key);
		}
		
		$query = $this->db->get ();
		
		if($single){
			$meta = $query->row();
		} else{		
			$meta = $query->result ();
			
			$process_meta = array();
			foreach($meta as $m){
				if(isset($process_meta[$m->meta_key])){
					if(!is_array($process_meta[$m->meta_key])){
						$meta_tmp = $process_meta[$m->meta_key];
						$process_meta[$m->meta_key] = array();
						array_push($process_meta[$m->meta_key], $meta_tmp);
					}
					
					array_push($process_meta[$m->meta_key], $m);
				} else{
					$process_meta[$m->meta_key] = $m;
				}
			}
			
			$meta = $process_meta;
		}
		
		return $meta;
	}
	
	function deleteMeta($element_id, $key, $value = NULL){
		$this->db->where($this->_field_meta_rel, $element_id);
		$this->db->where("meta_key", key);
		if($value !== NULL){
			$this->db->where("meta_value", $value);
		}
		$this->db->delete($this->_table_meta);
	}
	
	function upMeta($element_id, $key){
		$meta = $this->getMeta($element_id, $key, TRUE);
		
		if($meta == NULL){
			$value = 1;
			
			return $this->addMeta($element_id, $key, $value, TRUE);
		} else{
			$value = $meta->meta_value + 1;
			
			return $this->updateMeta($element_id, $key, $value);
		}		
	}
	
	function downMeta($element_id, $key){
		$meta = $this->getMeta($element_id, $key, TRUE);
		
		if($meta == NULL){
			$value = -1;
			
			return $this->addMeta($element_id, $key, $value, TRUE);
		} else{
			$value = $meta->meta_value + 1;
			
			return $this->updateMeta($element_id, $key, $value);
		}
	}
}
?>